//---------------------------------------------------------------------
//  This file is part of the Background Motion solution.
// 
//  Copyright (C) Mindscape (TM).  All rights reserved.
//  http://www.mindscape.co.nz
// 
//  THIS CODE AND INFORMATION ARE PROVIDED AS IS WITHOUT WARRANTY OF ANY
//  KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
//  IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
//  PARTICULAR PURPOSE.
//---------------------------------------------------------------------

using Mindscape.BackgroundMotion.Model;
using Mindscape.BackgroundMotion.Website.Controllers;
using Mindscape.BackgroundMotion.Website.Infrastructure;
using Mindscape.BackgroundMotion.Website.Presenters.Interfaces;

namespace Mindscape.BackgroundMotion.Website.Views
{
  public sealed class MemberPresenter : PresenterBase<IMemberView, IMembershipController>
  {
    /// <summary>
    /// This method is invoked by the view everytime it loads
    /// </summary>
    public override void OnViewLoaded()
    {
      string userId = Controller.GetParameter<string>(Constants.Parameters.UserID);

      if (userId != null)
      {
        LoadMemberDetails(Controller.GetMember(int.Parse(userId)));
      }
      else if (Controller.CurrentMember != null)
      {
        LoadMemberDetails(Controller.CurrentMember);
      }
    }


    /// <summary>
    /// Loads the member details on to the page.
    /// </summary>
    /// <param name="member">The member.</param>
    private void LoadMemberDetails(Member member)
    {
      int pageNumber = Controller.GetParameter<int>(Constants.Parameters.Page);

      // Only display unapproved contributions if the currently viewing user is the owner of this
      // profile, otherwise we don't render anything.
      if (member == Controller.CurrentMember)
      {
        
        View.MemberUnapprovedContributions = Controller.GetUnapprovedContributions(member);
        if (View.MemberUnapprovedContributions.TotalResults != 0)
        {
          View.HasUnapprovedContributions = true;
        }
      }
      
      View.MemberContributions = Controller.GetApprovedContributions(member, pageNumber);
      View.Member = member;
    }
  }
}